<?php
namespace app\cdyoa\controller;
use think\Db;
class Intui extends Base
{

    public function lists(){
        if( is_post() ){
            $page     = input('page');
            $keyword  = input('keyword');
            $status  = input('status');
            $min_time = input('min_time');
            $max_time = input('max_time');

            $sql = "
                SELECT
                    [1]
                FROM
                    in_tui a
                left join
                    admin_user b
                on
                    a.user_id = b.id
                left join
                    admin_user c
                on
                    a.action_user_id = c.id
                left join
                    orders e
                on
                    a.orders_id=e.id
                WHERE
                    1 = ?
                ";
            $param = [1];
            if ( !empty($keyword) ) {
                $sql .= " and (e.order_no like ? or e.title like ? or e.contract_no like ? or a.note like ?)";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
            }
            if ( $status != '100all' ) {
                $sql .= " and a.status = ?";
                $param[] = $status;
            }
            if ( !empty($min_time) ) {
                $sql    .= " and a.create_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $sql    .= " and a.create_time <= ?";
                $param[] = strtotime($max_time);
            }
            //分页
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            $page_size   = c('page_size');
            $page        = com_cur_page($total, $page,  $page_size);
            $page_data   = com_page_data($total, $page,  $page_size);

            //列表
            $start       = ($page - 1) * $page_size;
            $sql        .= " order by a.id desc limit $start, $page_size";
            $field       = "a.*,b.name,e.order_no,e.title,e.contract_no, c.name as aname";
            $sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);

            //格式数据
            foreach($list as $k => $v){
                $list[$k]['status']           =  c('type.intui_status',$v['status']);
                $list[$k]['bg_us']           =  number_format($v['bg_us'],2);
                $list[$k]['bg_rmb']           =  number_format($v['bg_rmb'],2);
                $list[$k]['rmb']           =  number_format($v['rmb'],2);
                $list[$k]['create_time']    =  com_format_time( $v['create_time'] );
                //操作按钮
                $list[$k]['action'] = "";
                if($v['status']==0||$v['status']==2){
                    $list[$k]['action'].=com_page(['ico'=>'del3','title'=>'删除收款','url'=>url('intui/delete'), 'post'=>['id'=>$v['id'],'t'=>'in_tui'],'confirm'=>"你确定要删除吗"]);
                }
            }
            $com_body    = com_build_table($list, array("id","name", "order_no" ,"title","contract_no","bg_us","bg_rmb","rmb", "note","aname","action_note", "status", "create_time","action"));
            $com_page    = com_build_page( $page_data );
            $data = array(
                "com_body"  => $com_body,
                "com_page"  => $com_page,
            );
            com_check(['data'=>$data]);
        }
        $data = [
            'nav'       => ['退税列表', '列表'],
            'status'    =>  ['100all'=>'全部状态'] + c('type.intui_status'),

        ];
        return view('', $data);
    }


    //添加退税
    public function add($id=''){
        if( is_post() ){
            $data = com_data(input(), ['id']);
            $rule = [
                'bg_rmb'      =>  'require|float',
                'rmb'      =>  'require|float',
            ];
            $msg = [];
            com_validate( $rule, $msg, $data, true );
            $data['orders_id']   = $id;
            $data['user_id']     = $this->userinfo['id'];
            $data['create_time'] = time();
            $data['bg_us'] = $data['bg_us'] ? $data['bg_us'] : 0;
            Db::name('in_tui')->insert($data);
            $config = [
                'msg' => '添加成功！',
                'suc' => [
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }
        $d = [
            'info' => Db::name('orders')->find($id),
            'id' => $id,
        ];
        return view('',$d);
    }

    public function delete(){
        if(is_post()){
            $id = input('id');
            $status  = Db::name('in_tui')->where('id',$id)->value('status');
            if( $status!=0 ){
                com_error(['已审核,不能删除']);
            }else{
                Db::name('in_tui')->delete( $id );
            }
            $config = [
                'suc' => [
                    'msg' => '删除成功！',
                    'js_action'=>"alert|reload",
                ],
            ];
            com_check( $config );
        }
    }
}
